package com.weishu.house.controller;


import com.github.pagehelper.PageInfo;
import com.weishu.common.core.domain.R;
import com.weishu.house.entity.SfContract;
import com.weishu.house.model.RequestSearchSfContractsDto;
import com.weishu.house.model.RequestSearchSfsDto;
import com.weishu.house.model.ResponseHouseInfoDto;
import com.weishu.house.service.ISfContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import com.weishu.common.core.web.controller.BaseController;
import com.weishu.house.entity.CwPaymentPlan;
import com.weishu.house.model.SfContractModel;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import java.math.BigDecimal;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gao
 * @since 2021-05-25
 */
@RestController
@Slf4j
@RequestMapping("/sf-contract")
public class SfContractController extends BaseController {

    @Autowired
    private ISfContractService iSfContractService;



    /**
     * 收房合同查询
     * @author gdw
     * @param sfContract
     * @return
     */
    @GetMapping("selectSfContract")
    public Object selectSfContract(SfContract sfContract) {
        startPage();
        List<SfContract> list = iSfContractService.selectSfContract(sfContract);
        return getDataTable(list);
    }

    /**
     * 根据id查询合同
     * @param sfContract
     * @return
     */
    @GetMapping("selectSfById")
    public Object selectSfById(SfContract sfContract) {
        SfContractModel byId = iSfContractService.selectSfById(sfContract.getId());
        List<CwPaymentPlan> cwPaymentPlanList = byId.getCwPaymentPlanList();
        //算出日租金
        BigDecimal divide = byId.getMonthRent().divide(new BigDecimal(30), 2);
        BigDecimal money = new BigDecimal("0.00");
        Date date = new Date();
        Long day = new Long(0);
        for (int i = 0; i <cwPaymentPlanList.size() ; i++) {
            CwPaymentPlan cwPaymentPlan = cwPaymentPlanList.get(i);

            if (cwPaymentPlan.getPayState() == 2) {

                if (cwPaymentPlan.getItemType() == 3) {
                    ZoneId zoneId = ZoneId.systemDefault();
                    ZonedDateTime zdt = cwPaymentPlan.getPaymentDaysEnd().atZone(zoneId);
                    Date date1 = Date.from(zdt.toInstant());
                    ZonedDateTime zdt2 = cwPaymentPlan.getPaymentDaysBegin().atZone(zoneId);
                    Date date2 = Date.from(zdt2.toInstant());
                    System.out.println(date1);
                    System.out.println(date2);
                    if (date.before(date1) || date.after(date2)) {
                        day=(date1.getTime()-date.getTime())/(24*60*60*1000);
                    }
                }else if(cwPaymentPlan.getItemType()==2){
                    money = money.add(cwPaymentPlan.getPaymentAmount());
                }else {
                    byId.setDepositMoney(new BigDecimal(0));
                }
            } else {
                byId.setDepositMoney(new BigDecimal(0));
            }
        }
        byId.setDepositMoney(money);
        byId.setAltogetherRent(divide.multiply(new BigDecimal(day)));
        return byId;
    }


    @GetMapping("list")
    public R listsfcontract (Integer pageNum, Integer pageSize, RequestSearchSfContractsDto
            requestSearchSfContractsDto){
        System.out.println();
        System.err.println(pageNum + "" + pageSize + "" + requestSearchSfContractsDto);
        PageInfo<SfContract> responseSearchSfContractsDtos = iSfContractService.searchSfContracts(pageNum, pageSize, requestSearchSfContractsDto);
        System.out.println(responseSearchSfContractsDtos);
        log.info("responseSearchSfContractsDtos");
        return R.ok(responseSearchSfContractsDtos);
    }


    @GetMapping("searchSfListByXy")
    public R searchSfListByXy(Integer pageNum, Integer pageSize, RequestSearchSfsDto requestSearchSfsDto){
        PageInfo<ResponseHouseInfoDto> responseSearchSfsDtos = iSfContractService.searchSfListByXy(pageNum,pageSize,requestSearchSfsDto);
        System.out.println(responseSearchSfsDtos);
        log.info("requestSearchSfsDto");
        return R.ok(responseSearchSfsDtos);
    }
}
